//
//  MJMyExamController.m
//  MJ
//
//  Created by qiaoBu on 2017/12/8.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJMyExamController.h"
#import "MJMyExamTableViewCell.h"
#import "MJMyInfoViewModel.h"
#import "MJMyHistoryExamsModel.h"
#import "MJAnswerViewController.h"
@interface MJMyExamController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *examTableView;
@property (nonatomic, strong) NSMutableArray *datasArr;

@end

@implementation MJMyExamController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navgationBar.titleLabel.text = @"我的测试";
    self.view.backgroundColor = RGBColor(242,242,242);
    
    [self commonInit];
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    [self requestExamsData];
}

- (void)requestExamsData{
    
     NSArray * array = [[NSUserDefaults standardUserDefaults] arrayForKey:MJ_HistoryExams];
    NSMutableString *examString = [[NSMutableString alloc] init];
    for (int i=0; i<array.count; i++) {
        
        NSString *answer = array[i];
        if (i==0) {
            
            examString = [NSMutableString stringWithFormat:@"%@",answer];
            
        }else{
         
            examString = [NSMutableString stringWithFormat:@"%@%@",@",",answer];
        }
    }
//    [MJMyInfoViewModel requestMyHistoryExamsSuccess:^(NSString *stateCode,MJMyHistoryExamsModel *model,NSString *messageState) {
//
//        if ([stateCode isEqualToString:@"001"])
//        {
//            self.datasArr = [NSMutableArray arrayWithArray:model.content];
//            [self.examTableView reloadData];
//        }
//        else
//        {
//            [TJMBProgressView showTingJianText:messageState];
//        }
//    } failure:^(NSError *error) {
//
//        [TJMBProgressView showTingJianText:networkError];
//    } netWorkState:^(BOOL netWorkState) {
//
//    }];
    
    [MJMyInfoViewModel requestMyHistoryExamsWithKey:examString Success:^(NSString *stateCode, MJMyHistoryExamsModel *model, NSString *messageState) {
        
        if ([stateCode isEqualToString:@"001"])
        {
            self.datasArr = [NSMutableArray arrayWithArray:model.content];
            [self.examTableView reloadData];
        }
        else
        {
            [TJMBProgressView showTingJianText:messageState];
        }
        
    } failure:^(NSError *error) {
        
         [TJMBProgressView showTingJianText:networkError];
        
    } netWorkState:^(BOOL netWorkState) {
        
    }];
}

- (void)commonInit{
    
    self.datasArr = [NSMutableArray array];
    [self.view addSubview:self.examTableView];
    self.examTableView.delegate = self;
    self.examTableView.dataSource = self;
    self.examTableView.separatorStyle = UITableViewCellSeparatorStyleNone;

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.datasArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cellID";
    MJMyExamTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[MJMyExamTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    ExamContentModel *model = self.datasArr[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    ExamContentModel *model = self.datasArr[indexPath.row];
    Anwser *answerModel = model.result;
    MJAnswerViewController *answerController = [[MJAnswerViewController alloc] init];
    answerController.anwser = answerModel;
    [self.navigationController pushViewController:answerController animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 90;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [UIView new];
}
- (UITableView *)examTableView{
    
    if (!_examTableView)
    {
        _examTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navgationBar.height+15, kScreenWidth, kScreenHeight-15-64) style:UITableViewStyleGrouped];
        _examTableView.backgroundColor = RGBColor(242,242,242);
        _examTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _examTableView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
